package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.DynamicContent;
import cn.wolfcode.trip.base.domain.Friend;
import cn.wolfcode.trip.base.domain.FriendPraise;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.FriendMapper;
import cn.wolfcode.trip.base.mapper.FriendPraiseMapper;
import cn.wolfcode.trip.base.query.FriendPraiseQueryObject;
import cn.wolfcode.trip.base.query.FriendQueryObject;
import cn.wolfcode.trip.base.service.IFriendPraiseService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class FriendPraiseServiceImpl implements IFriendPraiseService{
    @Autowired
    private FriendPraiseMapper friendPraiseMapper;
    @Autowired
    private FriendMapper friendMapper;

    /**
     * 新增一条点赞记录
     *
     * @param friendPraise
     */
    public void save(FriendPraise friendPraise) {
        friendPraiseMapper.insert(friendPraise);
    }

    /**
     * 删除一条点赞记录
     *
     * @param id
     */
    public void delete(Long id) {
        friendPraiseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询一条动态的点赞记录
     *
     * @param qo
     * @return
     */
    public List getListByDynamicId(FriendPraiseQueryObject qo) {
        return friendPraiseMapper.selectForList(qo);
    }

    /**
     * 点赞和取消操作，返回操作后的集合
     *
     * @param qo
     * @return
     */
    public String operatingPraise(FriendPraiseQueryObject qo) {
        FriendPraise tag = friendPraiseMapper.selectById(qo);
        if(tag==null){
            User user = new User();
            user.setId(qo.getUserId());
            DynamicContent dynamicContent = new DynamicContent();
            dynamicContent.setId(qo.getDynamicId());
            FriendPraise friendPraise = new FriendPraise(user,dynamicContent);
            friendPraiseMapper.insert(friendPraise);
        }else{
            friendPraiseMapper.deleteByUserIdAndDynamicContentId(qo);
        }
        List<FriendPraise> praiseList = friendPraiseMapper.selectForList(qo);
        //获取登录者的好友
        User user = UserContext.getCurrentUser();
        FriendQueryObject fqo = new FriendQueryObject();
        fqo.setUserId(qo.getUserId());
        List<Friend> currentFriend = friendMapper.selectForListByUser(fqo);
        HashSet friendSet = new HashSet();
        for (Friend friend : currentFriend) {
            friendSet.add(friend.getUser1().getId());
            friendSet.add(friend.getUser2().getId());
        }
        int count =0;
        String temp="";
        //比对点赞的人，是否在用户的好友里面，有就显示出来
        for (FriendPraise fp : praiseList) {
            count++;
            if(friendSet.contains(fp.getUser().getId())){
                temp+=fp.getUser().getNickName()+" ";
            }
        }
        if(count==0){
            temp="0人点赞";
        }else{
            temp+=" 等"+count+"人觉得赞";
        }
        return temp;
    }
}